<?php

namespace Home\Controller;
use Home\Controller\BaseController;

class StorejoininController extends BaseController {

    public function _initialize() {
        parent::_initialize();
    }

    public function index() {
        if(IS_POST){
            $captcha = I('captcha');
            $res = $this->check_verify($captcha);
            if($res){
                if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0-9]{1}[0-9]{8}$|18[0-9]{1}[0-9]{8}$/",trim(I('post.contacts_phone'),''))){
                    $this->error('手机号格式不正确');
                }
                $where['company_name'] = trim(I('post.company_name'),'');
                $where['contacts_phone'] = trim(I('post.contacts_phone'),'');
                $where['_logic'] = 'OR';
                $count = M('store_joinin')->where($where)->count();
                if($count > 0){
                    $this->error('您已提交过申请，无需重复提交');
                }
                $data = I('post.');
                $data['company_name'] = trim(I('post.company_name'),'');
                $data['contacts_phone'] = trim(I('post.contacts_phone'),'');
                $data['joinin_state'] = 10;
                $re = M('store_joinin')->data($data)->add();
                if($re){
                    $this->success('请等待客服与您联系',U('Index/index'));
                }else{
                    $this->error('失败请稍后重试');
                }
            }else{
                $this->error('验证码错误');
            }
        }
        $this->display();
    }

    // 生成验证码
    public function verify(){
        $config =    array(
            'fontSize'    =>    24,    // 验证码字体大小
            'length'      =>    4,     // 验证码位数
            'useNoise'    =>    false, // 关闭验证码杂点
            'imageW'      =>    0,     // 验证码宽度
            'imageH'      =>    0,     // 验证码高度
            'fontttf'     =>   '1.ttf',
        );
        $Verify = new \Think\Verify($config);
        $Verify->entry();
    }

    // 验证验证码
    public function check_verify($code){
        $verify = new \Think\Verify();
        return $verify->check($code);
    }

}
